package com.amar.dtadmin.apidoc.action;

import com.amar.dtadmin.apidoc.util.CurlUtil;
import com.amar.dtadmin.apidoc.util.NotificationUtils;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

/**
 * @author xiaoshiqiang
 * @since 2025/2/12
 */
public class ApiCopyAsCurlAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String curl = CurlUtil.generateCurl(DtAdminApiDocActionGroup.getSelectedPisData(e).getApiInfo());
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 创建 StringSelection 对象
        StringSelection selection = new StringSelection(curl);
        // 将 StringSelection 对象设置到剪贴板
        clipboard.setContents(selection, null);

        NotificationUtils.notifyInfo("Copy as cURL", "copied to clipboard");
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        e.getPresentation().setEnabledAndVisible(DtAdminApiDocActionGroup.getSelectedPisData(e).isValid());
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }
}
